//
//  DYProgressView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/6.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class DYProgressView: SYBaseView {
    
    var playOrPauseClosure: ((Bool)->Void)?
    var sliderChangeClosure: ((Float)->Void)?
    var beisuChangeClosure: ((CGFloat)->Void)?
    
    var beisus:[String] = ["0.5", "1.0", "1.25", "1.5", "2.0"]
    var beisuCount: Int = 1
    // 下载进度
    fileprivate var progress: UIProgressView = {
        let pro: UIProgressView = UIProgressView()
        pro.tintColor = UIColor(hex: 0x535353)
//        pro.trackTintColor = .white//UIColor(hex: 0x535353)
        return pro
    }()
    
    // 播放进度
    fileprivate var slider: UISlider = {
        let slid: UISlider = UISlider()
        slid.setThumbImage(UIImage(named: "icon_control_dot"), for: .normal)
        slid.addTarget(self, action: #selector(sliderAction), for: .touchUpInside)
        slid.maximumTrackTintColor = .clear
        slid.minimumTrackTintColor = UIColor(hex: 0xFFA02A)
        
        return slid
    }()
    
    fileprivate var playOrPauseImg: UIImageView = {
        let bt: UIImageView = UIImageView()
        bt.sy_name("jiangjie_pause")
        
        return bt
    }()
    
    var playOrPauseBg: UIView = {
        let v = UIView()
        v.layer.cornerRadius = 25.0.autoWidth()
        v.backgroundColor = UIColor(hex: 0xFFA02A)
        return v
    }()
    
    lazy var playOrPauseBt: UIButton = {
        let bt: UIButton = UIButton()
        bt.addTarget(self, action: #selector(playOrPauseAction), for: .touchUpInside)
        return bt
    }()
    
    fileprivate var currentTimeLb: UILabel = {
        let lb: UILabel = UILabel()
        lb.text = "00:00"
        lb.textColor = .white
        lb.font = UIFont.systemFont(ofSize: 13)
        lb.textAlignment = .center
        return lb
    }()
    
    fileprivate var totalTimeLb: UILabel = {
        let lb: UILabel = UILabel()
        lb.text = "00:00"
        lb.textColor = .white
        lb.font = UIFont.systemFont(ofSize: 13)
        lb.textAlignment = .center
        return lb
    }()
    
    let beisu: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("倍速播放", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 14.autoFontSize()
        v.layer.cornerRadius = 17.5.autoWidth()
        v.backgroundColor = UIColor(hex: 0x434343)
        return v
    }()
    
    let yuan: DYYuanxingProgress = {
        let v: DYYuanxingProgress = DYYuanxingProgress()
        
        return v
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    @objc func playOrPauseAction(sender: UIButton) {
        sender.isSelected = !sender.isSelected
        let vc = self.currentVC as! DYJiangjieListViewController
        vc.isPause = sender.isSelected
        self.playState(sender: sender)
        if let closure = playOrPauseClosure {
            closure(sender.isSelected)
        }
    }
    
    @objc func sliderAction(sender: UISlider) {
        DispatchQueue.main.async {
            self.yuan.setProgress(Int(100*sender.value), animated: true)
        }
        if let closure = sliderChangeClosure {
            closure(sender.value)
            print("sender.value ====== \(sender.value)")
        }
    }
    
    // 下载进度
    public func setProgress(float: Float) {
        self.progress.setProgress(float, animated: true)
    }
    
    public func setSlider(float: Float) {
        self.slider.setValue(float, animated: true)
        DispatchQueue.main.async {
            self.yuan.setProgress(Int(100*float), animated: true)
        }
    }
    
    public func setPlayState(play: Bool) {
        self.playOrPauseBt.isSelected = !play
        self.playState(sender: playOrPauseBt)
    }
    
    public func setCurrentTime(time: String) {
        self.currentTimeLb.text = time
    }
    
    public func setTotalTime(time: String) {
        self.totalTimeLb.text = time
    }
    
    fileprivate func playState(sender: UIButton) {
        
        if sender.isSelected {

            playOrPauseImg.sy_name("jiangjie_play")
            self.playOrPauseImg.snp.remakeConstraints { (make) in
                make.centerY.equalTo(playOrPauseBt)
                make.centerX.equalTo(playOrPauseBt).offset(3.autoWidth())
                make.width.equalTo(15.0.autoWidth())
                make.height.equalTo(19.0.autoWidth())
            }
        } else {
            playOrPauseImg.sy_name("jiangjie_pause")
            self.playOrPauseImg.snp.remakeConstraints { (make) in
                make.centerY.equalTo(playOrPauseBt)
                make.centerX.equalTo(playOrPauseBt)
                make.width.equalTo(15.0.autoWidth())
                make.height.equalTo(19.0.autoWidth())
            }
        }
        
    }
    
    override func initViews() {

        beisu.addAction {[weak self] in
            if let `self` = self {
                
                self.beisuCount += 1
                let current: String = self.beisus[self.beisuCount%5]
                self.beisu.setTitle(current, for: .normal)
                self.beisuChangeClosure?(CGFloat(current.floatValue))
            }
            
        }
        self.addSubview(self.playOrPauseBg)
        self.addSubview(self.playOrPauseImg)
        self.addSubview(self.playOrPauseBt)
        self.addSubview(self.currentTimeLb)
        self.addSubview(self.totalTimeLb)
        self.addSubview(self.progress)
        self.addSubview(self.slider)
        
        self.playOrPauseBg.addSubview(self.yuan)
        self.addSubview(self.beisu)
        
        self.playOrPauseImg.snp.makeConstraints { (make) in
            make.centerY.equalTo(playOrPauseBt)
            make.centerX.equalTo(playOrPauseBt)
            make.width.equalTo(15.0.autoWidth())
            make.height.equalTo(19.0.autoWidth())
        }
        
        self.playOrPauseBg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(66.0.autoWidth())
            make.centerX.equalToSuperview()
            make.width.height.equalTo(50.0.autoWidth())
        }
        
        self.playOrPauseBt.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(66.0.autoWidth())
            make.centerX.equalToSuperview()
            make.width.height.equalTo(50.0.autoWidth())
        }
        
        self.currentTimeLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(12.0.autoWidth())
            make.left.equalToSuperview().offset(16.0.autoWidth())
            make.height.equalTo(10.0.autoWidth())
        }
        
        self.totalTimeLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(12.0.autoWidth())
            make.right.equalToSuperview().offset(-16.0.autoWidth())
            make.height.equalTo(10.0.autoWidth())
        }
        
        self.progress.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(34.0.autoWidth())
            make.left.equalToSuperview().offset(15.0.autoWidth())
            make.right.equalToSuperview().offset(-15.0.autoWidth())
            make.height.equalTo(4)
        }
        
        self.slider.snp.makeConstraints { (make) in
            make.centerY.equalTo(progress)
            make.left.equalTo(self.progress.snp.left).offset(-1)
            make.right.equalTo(self.progress.snp.right)
            make.height.equalTo(35)
        }
        
        
        self.beisu.snp.makeConstraints { (make) in
            make.centerY.equalTo(playOrPauseBt)
            make.left.equalTo(self.playOrPauseBt.snp.right).offset(23.0.autoWidth())
            make.width.equalTo(75.0.autoWidth())
            make.height.equalTo(35.0.autoWidth())
        }
        self.yuan.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}


